package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.UseMoney;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;
import sanrenxing.dayEnd.backRepertoryTotal.service.UseMoneyBiz;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 编写者：
 * 日期：2021
 * 版本：1.0
 */
@RestController
@RequestMapping("/UseMoney")
public class UseMoneyController {
    @Autowired
    private UseMoneyBiz useMoneyBiz;
    //自动装配  业务处理方法 改
    @Autowired
    private TransactionService transactionService;

    @GetMapping("/usemoney")
    public Map<String, Object> addUseMoney(HttpServletRequest req, int page, int limit) {
        //定义多条件查询对象
        UseMoney useMoney = new UseMoney();
        //接收请求中的交易日期
        String tranDate = req.getParameter("tranDate");
        //判断是否加入
        if (tranDate != null && tranDate.trim().length() != 0) {
            useMoney.setTranDate(tranDate);
        }
        //调用业务层多条件查询方法
        String condition = useMoneyBiz.getCount(useMoney);
        //执行多条件查询语句
        List<UseMoney> useMonies = useMoneyBiz.findByPages(limit, page, condition);
        //获取总记录行数
        int totalRows = useMoneyBiz.count(condition);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        //证券信息 改
        map.put("securi",transactionService.SecuritiesInfomationLXF());
        map.put("data", useMonies);
        map.put("count", totalRows);

        return map;
    }

    @DeleteMapping("/usemoney/{seId}")
    public String delete(String seId) {
        //调用业务层代码删除数据
        int rows = useMoneyBiz.delete(seId);
        return rows > 0 ? "success" : "failure";
    }

    @PutMapping("/batchDel")
    public String batchDel(String seIdStr) {
        //对字符串按","分割
        String[] seIds = seIdStr.split(",");
        //定义返回值
        String result = "delete success";
        //循环删除
        for (String str : seIds) {
            //调用业务层删除记录
            int rows = useMoneyBiz.delete(str);
            //判断
            if (rows < 0) {
                result = "delete failure";
                break;
            }
        }
        return result;
    }
}
